package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.domain.YyClass;
import com.ruoyi.system.service.IYyTeacherService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YyCourse;
import com.ruoyi.system.service.IYyCourseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程信息Controller
 *
 * @author ruoyi
 * @date 2024-03-12
 */
@Controller
@RequestMapping("/system/course")
public class YyCourseController extends BaseController
{
    private String prefix = "system/course";

    @Autowired
    private IYyCourseService yyCourseService;

    @Autowired
    private IYyTeacherService yyTeacherService;

    @RequiresPermissions("system:course:view")
    @GetMapping()
    public String course()
    {
        return prefix + "/course";
    }


    /**
     * 查询课程列表
     */
    @PostMapping("/listAll")
    @ResponseBody
    public AjaxResult listAll()
    {
        List<YyCourse> list = yyCourseService.listAll();
        return AjaxResult.success(list);
    }


    /**
     * 查询课程信息列表
     */
    @RequiresPermissions("system:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YyCourse yyCourse)
    {
        startPage();
        List<YyCourse> list = yyCourseService.selectYyCourseList(yyCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程信息列表
     */
    @RequiresPermissions("system:course:export")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YyCourse yyCourse)
    {
        List<YyCourse> list = yyCourseService.selectYyCourseList(yyCourse);
        ExcelUtil<YyCourse> util = new ExcelUtil<YyCourse>(YyCourse.class);
        return util.exportExcel(list, "课程信息数据");
    }

    /**
     * 新增课程信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("tea", yyTeacherService.listAll());
        return prefix + "/add";
    }

    /**
     * 新增保存课程信息
     */
    @RequiresPermissions("system:course:add")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YyCourse yyCourse)
    {
        return toAjax(yyCourseService.insertYyCourse(yyCourse));
    }

    /**
     * 修改课程信息
     */
    @RequiresPermissions("system:course:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        YyCourse yyCourse = yyCourseService.selectYyCourseById(id);
        mmap.put("yyCourse", yyCourse);
        mmap.put("tea", yyTeacherService.selectYyTeacherByIdList(Long.parseLong(String.valueOf(yyCourse.getTeaId()))));
        return prefix + "/edit";
    }

    /**
     * 修改保存课程信息
     */
    @RequiresPermissions("system:course:edit")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YyCourse yyCourse)
    {
        return toAjax(yyCourseService.updateYyCourse(yyCourse));
    }

    /**
     * 删除课程信息
     */
    @RequiresPermissions("system:course:remove")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(yyCourseService.deleteYyCourseByIds(ids));
    }
}
